#include <iostream>
#include <vector>
using namespace std;

void dfs(vector<vector<int>> &gh, vector<int> &dist, vector<int> &last, int u, int p, int w)
{
	last[u] = p;
	dist[u] = w;
	for (auto v : gh[u]) {
		if (v != p) {
			dfs(gh, dist, last, v, u, w + 1);
		}
	}
}

int road(vector<vector<int>> &gh, vector<int> &dist, vector<int> &last, vector<bool> &path)
{
	int start = 1, end = 1;
	int n = dist.size() - 1;
	dfs(gh, dist, last, 1, 0, 0);
	for (int i = 2; i <= n; i++) {
		if (dist[i] > dist[start]) {
			start = i;
		}
	}
	dfs(gh, dist, last, start, 0, 0);
	for (int i = 2; i <= n; i++) {
		if (dist[i] > dist[end]) {
			end = i;
		}
	}

	for (int u = end; u; u = last[u]) {
		path[u] = true;
	}

	return dist[end];
}

int dp_meter(vector<vector<int>> &gh, vector<int> &dist, vector<bool> &path, int u, int p)
{
	dist[u] = 0;
	int diameter = 0;
	for (auto v : gh[u]) {
		if (v != p) {
			int d = path[u] && path[v] ? -1 : 1;
			diameter = max(diameter, dp_meter(gh, dist, path, v, u));
			diameter = max(diameter, dist[u] + dist[v] + d);
			dist[u] = max(dist[u], dist[v] + d);
		}
	}
	return diameter;
}

int main()
{
	int n = 0, k = 0;
	cin >> n >> k;
	vector<vector<int>> gh(n + 1);
	vector<bool> path(n + 1, false);
	vector<int> last(n + 1, 0), dist(n + 1, 0);

	for (int i = 0, u, v; i < n - 1; i++) {
		cin >> u >> v;
		gh[u].push_back(v);
		gh[v].push_back(u);
	}
	int diameter = road(gh, dist, last, path);
	if (k == 1) {
		cout << 2 * (n - 1) - diameter + 1 << endl;
	} else {
		int diameter_sec = dp_meter(gh, dist, path, 1, 0);
		cout << (n - 1) * 2 - diameter - diameter_sec + 2 << endl;
	}

	return 0;
}